/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2003 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */

using System;
using System.Collections;
using System.IO;
using System.Diagnostics;


namespace UGed
{

   public class Tools
   {

      public static void GetDates(string sDateText, ref DateTime dtActualDate, ref int iYear, ref bool bIsAbout, ref int iEstimateStyle)
      {
         int iStringLength;
         int iOffset;
         string sTemp = "0";
         //VbStrings.Functions vf = new VbStrings.Functions();
         StringFuncs sf = new StringFuncs();


         dtActualDate = System.DateTime.FromOADate(0);
         iYear = 0;
         bIsAbout = false;
         iEstimateStyle = 0;

         if (sDateText == null)
         {
            dtActualDate = System.DateTime.FromOADate(0);
            iYear = 0;
            bIsAbout = false;
            iEstimateStyle = 0;
            return;
         }


         if (sDateText.Length == 0)
         {
            dtActualDate = System.DateTime.FromOADate(0);
            iYear = 0;
            bIsAbout = false;
            iEstimateStyle = 0;
            return;
         }

         iStringLength = sf.Len(sDateText);
         for (iOffset = iStringLength; iOffset <= 1; iOffset--)
         {// Find	the next	delimiter.
            if (sf.Mid(sDateText, iOffset, 1) == "/" || sf.Mid(sDateText, iOffset, 1) == "\\")
            {
               if (iOffset > 4)
               {
                  sTemp = sf.Mid(sDateText, iOffset - 4, 4);
                  bIsAbout = true;
                  iEstimateStyle = 1;
                  break;
               }
            }
         } // iOffset

         if (sf.IsDate(sDateText) == true)
         {
            dtActualDate = sf.DateValue(sDateText);
            iYear = dtActualDate.Year;
            bIsAbout = false;
         }
         else
         {
            //handle	abt and est
            //sDateText
            dtActualDate = System.DateTime.FromOADate(0);

            if (sf.UCase(sf.Left(sDateText, 3)) == "ABT")
            {
               sDateText = sf.Trim(sf.Right(sDateText, sf.Len(sDateText) - 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 1;
            }

            if (sf.UCase(sf.Right(sDateText, 5)) == "(EST)")
            {
               sDateText = sf.Trim(sf.Left(sDateText, 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 1;
            }

            //ends with	(calc) and does not begin with between
            if (sf.UCase(sf.Right(sDateText, 6)) == "(CALC)" && (sf.UCase(sf.Left(sDateText, 3)) != "BET"))
            {
               sDateText = sf.Trim(sf.Left(sDateText, 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 1;
            }

            if (sf.UCase(sf.Left(sDateText, 3)) == "BEF")
            {
               sDateText = sf.Trim(sf.Right(sDateText, sf.Len(sDateText) - 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 4;
            }

            if (sf.UCase(sf.Left(sDateText, 3)) == "AFT")
            {
               sDateText = sf.Trim(sf.Right(sDateText, sf.Len(sDateText) - 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 5;
            }

            if (sf.UCase(sf.Left(sDateText, 3)) == "BET")
            {
               sDateText = sf.Trim(sf.Right(sDateText, sf.Len(sDateText) - 8));
               sDateText = sf.Trim(sf.Left(sDateText, 4));
               if (sf.IsDate(sDateText) == true)
               {
                  dtActualDate = sf.DateValue(sDateText);
                  iYear = dtActualDate.Year;
               }
               bIsAbout = true;
               iEstimateStyle = 6;
            }

            if (System.Convert.ToInt32(sTemp) == 0)
            {
               //see	if	the last	four characters are digits
               sTemp = sf.Right(sDateText, 4);
               try
               {
                  if (System.Convert.ToInt32(sTemp) != 0)
                  {
                     sDateText = sTemp;
                  }
               }
               catch { }
            }
            else
            {
               sDateText = sTemp;
            }
            if (iYear == 0)
            {
               try
               {
                  iYear = System.Convert.ToInt32(sf.Trim(sf.Right(sDateText, 4)));
               }
               catch
               {
                  iYear = 0;
               }

            }
         }
      }


      //		public static Array Resize(Array	array, int newSize)
      //		{
      //			Type type =	array.GetType();
      //			Array	newArray	= Array.CreateInstance(type.GetElementType(), newSize);
      //			Array.Copy(array,	0,	newArray, 0, Math.Min(newArray.Length,	newSize));
      //			return newArray;
      //		}


      public static string GetPath()
      {
         string sPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
         return sPath;
      }

      public static void Logit(string sLogMsg)
      {
         //VbStrings.Functions vf = new VbStrings.Functions();

         string timeStamp;
         timeStamp = DateTime.Now.ToString("HH:mm:ss.fff");
         Trace.WriteLine(timeStamp + "	- " + sLogMsg);
         Console.WriteLine(sLogMsg);
      }

      public static void SilentLogit(string sLogMsg)
      {
         //VbStrings.Functions vf = new VbStrings.Functions();
         string timeStamp;
         timeStamp = DateTime.Now.ToString("HH:mm:ss.fff");
         Trace.WriteLine(timeStamp + "	- " + sLogMsg);
      }

   }
}
